package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.CustomerReturnListService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * description
 *
 * @author SZ
 * @date 2022/9/26/9:42
 */
@RestController
@RequestMapping("/customerReturnListGoods")
public class CustomerReturnListGoodsController {
    @Autowired
    private CustomerReturnListGoodsService customerReturnListGoodsService;
    @Autowired
    private CustomerReturnListService customerReturnListService;
    @PostMapping("/save")
    public ServiceVO save(CustomerReturnList customerReturnList,
                          String customerReturnListGoodsStr,
                          HttpSession session){
        User user = (User) session.getAttribute ("currentUser");
        Integer userId = user.getUserId ();
        customerReturnList.setUserId (userId);
        Gson gson = new Gson ();
        List list = gson.fromJson (customerReturnListGoodsStr, List.class);
        List<CustomerReturnListGoods> customerReturnListGoodsList = new ArrayList<> ();
        for (Object o : list) {
            String json = gson.toJson (o);
            CustomerReturnListGoods customerReturnListGoods = gson.fromJson (json, CustomerReturnListGoods.class);
            customerReturnListGoodsList.add (customerReturnListGoods);
        }
        return customerReturnListGoodsService.save(customerReturnList,customerReturnListGoodsList);

    }

    @PostMapping("/list")
    public List<CustomerReturnList> getList(String returnNumber, Integer customerId, Integer state, String sTime,String eTime){
        return customerReturnListService.getList(returnNumber,customerId,state,sTime,eTime);
    }
    @PostMapping("/goodsList")
    public List<CustomerReturnListGoods> getGoodsList(Integer customerReturnListId){
        return customerReturnListGoodsService.getGoodsList(customerReturnListId);
    }
    @PostMapping("/delete")
    public ServiceVO delete(Integer customerReturnListId){
        return customerReturnListGoodsService.delete(customerReturnListId);
    }
}
